﻿using System;
using System.ComponentModel;
using System.Workflow.Activities;
using System.Workflow.ComponentModel;
using ApplicationGenerator.Versions.BusinessLogic;
using ApplicationGenerator.Versions.Forms;
using System.Drawing.Design;
using System.Collections.Generic;


namespace WorkflowLibrary
{
    public class GeneratorSequentialWorkflow : SequentialWorkflowActivity, IActivityName//, ICustomTypeDescriptor
    {

        #region Variables

        private static Guid _workflowType = new Guid();
        private int _instanceId = -1;
        private VersionInfo pageVersionInfo;

        #endregion

        #region Properties

        [Category("Dane podstawowe")]
        [DisplayName("Model")]
        [Description("")]
        [TypeConverter(typeof(VersionConverter))]
        [Editor(typeof(VersionEditor), typeof(UITypeEditor))]
        [VersionType(VersionElement.Model)]
        public VersionInfo MetaModel
        {
            get;
            set;
        }

        [Description("The counter to increment")]
        [Category("Custom")]
        public VersionInfo PageVersionInfo
        {
            get { return pageVersionInfo; }
            set { pageVersionInfo = value; }
        }

        public static Guid WorkflowType
        {
            get { return _workflowType; }
        }

        public int InstanceId
        {
            get { return _instanceId; }
            set { _instanceId = value; }
        }

        #endregion

        #region Construction

        public GeneratorSequentialWorkflow()
        {
            Console.WriteLine("--- generatorseqwf: creating ---");
            this.Initialized += new EventHandler(GeneratorSequentialWorkflow_Initialized);
        }

        #endregion

        #region Handlers

        public void GeneratorSequentialWorkflow_Initialized(object sender, EventArgs e)
        {
            Console.WriteLine("--- attepmting to connect handlers to activites ---");
            foreach (Activity activity in Activities)
            {
                try
                {
                    if (typeof(ShowFormActivity).IsAssignableFrom(activity.GetType()))
                    {
                        ((ShowFormActivity)activity).FormShown += new ShowFormActivity.ShowFormEventHandler(GeneratorSequentialWrokflow_FormShown);
                    }
                    else if (typeof(StopProcessActivity).IsAssignableFrom(activity.GetType()))
                    {
                        ((StopProcessActivity)activity).ProcessStopped += new StopProcessActivity.StopProcessEventHandler(GeneratorSequentialWrokflow_StopProcessShown);
                    }
                    /*else if (typeof(DisplayPageActivity).IsAssignableFrom(activity.GetType()))
                    {
                        ((DisplayPageActivity)activity).PageShown += new DisplayPageActivity.PageShownEventHandler(GeneratorSequentialWrokflow_PageShown);
                    }*/
                }
                catch (Exception)
                {
                    //in case of any error do nothing with this
                    Console.WriteLine("--- error during adding event handler! --- ");
                }

            }
        }

        private void GeneratorSequentialWrokflow_FormShown(object sender, WorkflowLibrary.ShowFormEventArgs e)
        {
            //Console.WriteLine("--- wywolany FormShown [fg:"+  e.FormGuid.ToString() +",wfg:"+ e.WorkflowInstanceId+",sg:"+e.StructureId+" ---");
            pageVersionInfo = e.FormVersionInfo;

        }

        private void GeneratorSequentialWrokflow_StopProcessShown(object sender, WorkflowLibrary.StopProcessEventArgs e)
        {
            //Console.WriteLine("--- wywolany FormShown [fg:"+  e.FormGuid.ToString() +",wfg:"+ e.WorkflowInstanceId+",sg:"+e.StructureId+" ---");
            pageVersionInfo = e.VersionInfo;

        }


        /*private void GeneratorSequentialWrokflow_PageShown(object sender, WorkflowLibrary.PageShownEventArgs e)
        {
            Console.WriteLine("--- wywolany FormShown [fg:" + e.FormGuid.ToString() + ",wfg:" + e.WorkflowInstanceId + ",sg:" + e.StructureId + " ---");
            _formGuid = e.FormGuid;
        }*/

        #endregion

        #region ICustomTypeDescriptor Members

        public AttributeCollection GetAttributes()
        {
            return TypeDescriptor.GetAttributes(this, true);
        }

        public string GetClassName()
        {
            return TypeDescriptor.GetClassName(this, true);
        }

        public string GetComponentName()
        {
            return TypeDescriptor.GetComponentName(this, true);
        }

        public TypeConverter GetConverter()
        {
            return TypeDescriptor.GetConverter(this, true);
        }

        public EventDescriptor GetDefaultEvent()
        {
            return TypeDescriptor.GetDefaultEvent(this, true);
        }

        public PropertyDescriptor GetDefaultProperty()
        {
            return TypeDescriptor.GetDefaultProperty(this, true);
        }

        public object GetEditor(Type editorBaseType)
        {
            return TypeDescriptor.GetEditor(this, editorBaseType, true);
        }

        public EventDescriptorCollection GetEvents()
        {
            return TypeDescriptor.GetEvents(this, true);
        }

        public EventDescriptorCollection GetEvents(Attribute[] attributes)
        {
            return TypeDescriptor.GetEvents(this, attributes, true);
        }

        public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            PropertyDescriptorCollection colAvailableCollection = TypeDescriptor.GetProperties(this, attributes, true);
            List<PropertyDescriptor> colDisplayProperties = new List<PropertyDescriptor>();
            colDisplayProperties.Add(colAvailableCollection["ActivityName"]);
            colDisplayProperties.Add(colAvailableCollection["MetaModel"]);
            return new PropertyDescriptorCollection(colDisplayProperties.ToArray());
        }

        public PropertyDescriptorCollection GetProperties()
        {
            return TypeDescriptor.GetProperties(this, true);
        }

        public object GetPropertyOwner(PropertyDescriptor pd)
        {
            return this;
        }

        #endregion

        #region IActivityName Members

        [Category("Dane podstawowe")]
        [DisplayName("Nazwa procesu")]
        [Description("Nazwa procesu przyjazna dla użytkownika")]
        public string ActivityName
        {
            get;
            set;
        }

        #endregion
    }

}
